<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-input v-decorator="['id']" hidden="true"></a-input>
        <a-row>
          <a-col :span="24">
            <a-form-item label="合同号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractCode']" placeholder="请输入合同号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractName']" placeholder="请输入合同名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <depart-tree-modal ref="departModel" @ok="onDepartOk"></depart-tree-modal>
    <supplier-list-modal ref="supplierModal" @ok="onSupplierOk"></supplier-list-modal>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@lone/lone-vue/src/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@lone/lone-vue/src/utils/util'
  import JFormContainer from '@lone/lone-vue/src/components/jeecg/JFormContainer'
  import JDate from '@lone/lone-vue/src/components/jeecg/JDate'
  import SupplierListModal from '../../base/form/SupplierListModal'
  import DepartTreeModal from '../../base/form/DepartTreeModal'

  export default {
    name: 'MmsContractForm',
    components: {
      JFormContainer,
      JDate,
      SupplierListModal,
      DepartTreeModal
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        isTicket: false,
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/base/mmsContract/add",
          edit: "/base/mmsContract/edit",
          queryById: "/base/mmsContract/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      handleChange(e) {
        this.isTicket = e.target.checked;
        this.$nextTick(() => {
          this.form.validateFields(['isTicket'], { force: true });
        });
      },
      onSearchSupplier (){
        this.$refs.supplierModal.showSupplierInfo();
      },
      onSupplierOk (selectionRows, num) {
        this.form.setFieldsValue({supplierName: selectionRows.name})
        this.form.setFieldsValue({supplierCode: selectionRows.code})
      },
      onSearchDepart (){
        this.$refs.departModel.showSelectDepartTreeModal('ok');
      },
      onDepartOk (selectionRows, num) {
        this.form.setFieldsValue({mainDepartId: selectionRows.key})
        this.form.setFieldsValue({mainDepart: selectionRows.label})
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,"id", 'contractCode','contractName','supplierId','supplierName','status','startTime','endTime','remarks','mainDepartId','mainDepart','isTicket','createBy','createTime','updateBy','updateTime'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            that.$emit('ok', formData);
            that.confirmLoading = false;
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,"id", 'contractCode','contractName','supplierId','supplierName','status','startTime','endTime','remarks','mainDepartId','mainDepart','isTicket','createBy','createTime','updateBy','updateTime'))
      },
    }
  }
</script>